package com.mazegame.frontend;

import com.mazegame.backend.BackgroundPanel;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;

import javax.swing.*;
import java.awt.*;

public class BufferPanel extends JPanel implements GamePanel {
    private static CardLayout cardLayout;
    private static JPanel cardPanel;
    private JLayeredPane layeredPane;
    private static final int WIDTH = 1500;
    private static final int HEIGHT = 900;
    private JFXPanel jfxPanel;
    public static int num;
    public static boolean isFirst = true;

    public BufferPanel(CardLayout cardLayout, JPanel cardPanel) {
        this.cardLayout = cardLayout;
        this.cardPanel = cardPanel;
        initializeUI();
    }

    private void initializeUI() {
        // 使用 JLayeredPane 管理不同层级的组件
        layeredPane = new JLayeredPane();
        setLayout(new BorderLayout());
        add(layeredPane, BorderLayout.CENTER);
        // 创建背景面板并添加到最底层
        BackgroundPanel backgroundPanel = new BackgroundPanel("/img/黑色背景.png");
        backgroundPanel.setBounds(0, 0, WIDTH, HEIGHT);
        layeredPane.add(backgroundPanel, JLayeredPane.DEFAULT_LAYER);
        // 创建 JavaFX 内容面板
        jfxPanel = new JFXPanel();
        jfxPanel.setOpaque(false); // 确保 JFXPanel 透明
        jfxPanel.setBounds(0, 0, WIDTH, HEIGHT);
        layeredPane.add(jfxPanel, JLayeredPane.MODAL_LAYER); // 放在上层
        Platform.runLater(() -> {
            // 创建主容器
            BorderPane root = update();
            //设置root透明
            root.setStyle("-fx-background-color: rgba(0, 0, 0, 0);");
            // 创建透明场景
            Scene scene = new Scene(root, WIDTH, HEIGHT, javafx.scene.paint.Color.TRANSPARENT);
            jfxPanel.setScene(scene);
        });
    }
    public static BorderPane update() {
        BorderPane root = new BorderPane();
        javafx.scene.control.Label tip = new Label("点击任意位置继续...");
        tip.setFont(Font.font("KaiTi", FontWeight.BOLD, 48));
        tip.setTextFill(Color.WHITE);
        root.setOnMouseClicked(event -> {
            if(isFirst){
                if(num==1){
                    cardLayout.show(cardPanel, "FIRSTSTORYPANEL");
                    FirstStoryPanel.startFirstStory4();
                } else if (num==2) {
                    cardLayout.show(cardPanel, "SECONDSTORYPANEL");
                    SecondStoryPanel.startFirstStory3();
                } else if (num==3) {
                    cardLayout.show(cardPanel, "THIRDSTORYPANEL");
                    ThirdStoryPanel.startFirstStory2();
                }
                isFirst = false;
            }
        });
        root.setCenter(tip);
        return root;
    }
    @Override
    public void showPanel() {
        cardLayout.show(cardPanel, "BUFFERPANEL");
    }
    @Override
    public void cleanup() {}
}
